package com.itfitness.openglcamera.helper;

import android.util.Rational;

import androidx.camera.core.CameraX;
import androidx.camera.core.Preview;
import androidx.camera.core.PreviewConfig;
import androidx.lifecycle.LifecycleOwner;

public class CameraHelper {
    private static CameraX.LensFacing lensFacing = CameraX.LensFacing.BACK;

    public CameraHelper(LifecycleOwner lifecycleOwner, Preview.OnPreviewOutputUpdateListener onPreviewOutputUpdateListener) {
        CameraX.bindToLifecycle(lifecycleOwner, getPreview(onPreviewOutputUpdateListener));
    }

    private static Preview getPreview(Preview.OnPreviewOutputUpdateListener listener) {
        PreviewConfig previewConfig = new PreviewConfig.Builder()
                .setTargetAspectRatio(Rational.NaN)
//                .setTargetResolution(new Size(1280, 720))
                .setLensFacing(lensFacing)
                .build();
        Preview preview = new Preview(previewConfig);
        preview.setOnPreviewOutputUpdateListener(listener);
        return preview;
    }
}
